
const { readFileSync, existsSync } = require('fs')
const path = require('path')
const { parse } = require('dotenv')

/**
 * 把env文件的值设置到全局process.env中，只做增量不修改
 * @param {env文件路径} envPath 
 */
const generateEnv = (envPath) => {
    const getPath = envPath || path.resolve(process.cwd(), '.env')
    if (existsSync(getPath)) {
        const fileContentStr = readFileSync(getPath, 'utf-8')
        if (typeof fileContentStr === 'string') {
            const parsedEnv = parse(fileContentStr)
            console.log('parsedEnv', parsedEnv);
            for (let key in parsedEnv) {
                if (!process.env.hasOwnProperty(key)) {
                    process.env[key] = parsedEnv[key]
                }
            }
        }
    }
}



module.exports = generateEnv
